var proId;
function exit(){
	$("#opDate").val("");
	$("#opTime").val("");
	$("#operUser").val("");
	$("#proState").val("");
	$("#proId").val("");
	load();
	//关闭所有页面层
	layer.closeAll('page');
}
layui.use(['form', 'layedit', 'laydate','table'], function(){
	var table = layui.table
	,laydate = layui.laydate
	,form = layui.form
	,layer = layui.layer
	,layedit = layui.layedit;

	//日期
	laydate.render({
		elem: '#date'
	});

	laydate.render({
		elem: '#time'
	});

//	日期时间选择器
	laydate.render({
		elem: '#opDate'
		,type: 'datetime'
		,min:minDate()
	});

//	日期时间选择器
	laydate.render({
		elem: '#opTime'
		,type: 'datetime'
		,min:minDate()
	});

	// 设置最小可选的日期
	function minDate(){
	    var now = new Date();
	    return now.getFullYear()+"-" + (now.getMonth()+1) + "-" + now.getDate();
	 }
	
	//监听工具条
	table.on('tool(demo)', function(obj){
		var da = obj.data;

		if(obj.event === 'edit'){
			//修改
			var url="../../production/findById.action";
			var data={"proId":da.proId};
			$.post(url,data,function(mes){
				layer.open({
					type: 1	 //此处以iframe举例
					,title: '数据修改'
						,area: ['500px', '400px']
				,offset: 'auto'
					,content:$('#addOrUpdate')
					,success: function(layero){
						$("#opDate").val(mes.proDate);
						$("#opTime").val(mes.proTime);
						$("#operUser").val(mes.oper);
						$("#proState").val(mes.proState);
						$("#proId").val(mes.proId);
						$("#operUser").attr({"disabled":true});
						//渲染
						load();
					},end : function() {
						$("#operUser").attr({"disabled":false});
						//弹框关闭的回调函数
						$("#addOrUpdate").hide();
						exit();
					}
				});

			},"json");
		}else if(obj.event === 'del'){
			layer.confirm('真的删除行么',{title:"确认操作"}, function(index){
				var index = layer.load(0, {shade: false});
				//删除
				var url="../../production/delete.action";
				var data={"proId":da.proId}
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						obj.del();
						layer.msg("删除成功");
						//重载表单
						//active['reload'] ? active['reload'].call(this) : '';
					}else{
						layer.msg("删除失败");
					}
				},"json");

			});
		}else if(obj.event === 'detail'){
			//添加详情
			layer.open({
				type: 2	 //此处以iframe举例
				,title: '添加详情'
					,area: ['1000px', '490px']
			,offset: 'auto'
				,content:['../../page/cst/snprodetail.html?proId='+da.proId+'','on']
			,end : function() {
				//alert("关闭");
			}
			});
		}else if(obj.event === 'cxdetail'){
			//查看详情
			$("#cxDetailDivProId").val(da.proId);
			layer.open({
				type: 1	 //此处以iframe举例
				,title: '生产详情'
					,area: ['800px', '490px']
			,offset: 'auto'
				,content:$('#cxDetailDiv')
				,success: function(layero){
					//执行重载
					table.reload('cxDetailTable', {
						page: {
							curr: 1 //重新从第 1 页开始
						},where: {
							proId: $("#cxDetailDivProId").val(),
						}
					});
				},end:function(){
					$("#cxDetailDiv").css({"display":"none"});
				}
			});
		}else if(obj.event=='make'){
			proId=da.proId;
			//开始制作
			$("#cxDetailDivProId").val(da.proId);
			layer.open({
				type: 1	 //此处以iframe举例
				,title: '制作详情'
					,area: ['800px', '490px']
			,offset: 'auto'
				,content:$('#manufactureDiv')
				,success: function(){
					//执行重载
					table.reload('manuTable', {
						page: {
							curr: 1 //重新从第 1 页开始
						},where: {
							proId: da.proId,
						}
					});

					$("#manuTableTwoBody").html("");
					var url="../../snmanufacture/query.action";
					var data={"proId":da.proId};
					$.post(url,data,function(mes){
						$.each(mes,function(index,item){
							$("#manuTableTwoBody").append("<tr>" +
									"<td>"+item.comName+"</td>" +
									"<td><input type='text' class='manuNumber' value="+item.smUndone+"></td>" +
									"<td><input type='text' id="+item.smId+" class='manuSum' value='0' onkeyup='findKeyup(\""+item.smId+"\")'></td>" +
							"</tr>");
						});
					},"json");
					load();
				},end:function(){
					manuExit();
					$("#manufactureDiv").css({"display":"none"});
				}
			});
		}else if(obj.event=='addQuality'){
			layer.confirm('请确认制作完成',{title:"确认操作"}, function(index){
				var index = layer.load(0, {shade: false});
				//提交质检
				var url="../../quality/addAll.action";
				var data={"proId":da.proId};
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						layer.msg("提交质检成功");//重载表单
						active['reload'] ? active['reload'].call(this) : '';
					}else{
						layer.msg("提交质检成功");
					}
				},"json");
			});
		}else if(obj.event=='accomplish'){
			
			layer.confirm('请确认制作完成',{title:"确认操作"}, function(index){
				var index = layer.load(0, {shade: false});
				//制作完成
				var url="../../production/updateState.action";
				var data=data={"proId":da.proId,"state":4};
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						layer.msg("制作完成");
						//重载表单
						active['reload'] ? active['reload'].call(this) : '';
					}else{
						layer.msg("完成失败");
					}
				},"json");
			});


		}else if(obj.event=='audit'){
			layer.confirm('请确认提交审核',{title:"确认操作"}, function(index){
				var index = layer.load(0, {shade: false}); 
				//提交审核
				var url="../../prodetail/findProId.action";
				var data={"proId":da.proId};
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==""){
						layer.msg("您还没有选择需要的商品");
					}else{
						var url="../../audit/addProduction.action";
						var data={"proId":da.proId};
						$.post(url,data,function(mes){
							if(mes==1){
								layer.msg("提交成功,请耐心等待");
								//重载表单
								active['reload'] ? active['reload'].call(this) : '';
							}else{
								layer.msg("提交失败");
							}
						},"json");
					}
				},"json");
			});
		}
	});


	var $ = layui.$, active = {
			reload: function(){
				var date = $('#date');
				var time = $('#time');
				var oper = $('#oper');
				var state= $('#state');
				//执行重载
				table.reload('idTest', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				,where: {
					proDate: date.val(),
					proTime: time.val(),
					oper: oper.val(),
					proState:state.val()
				}
				});
			},
			manuSelect:function(){
				var comName = $('#comNameInput');
				//执行重载
				table.reload('manuTable', {
					page: {
						curr: 1 //重新从第 1 页开始
					}
				,where: {
					comName: comName.val(),
					proId:proId
				}
				});
			},
			manuBao:function(){
				var index = layer.load(0, {shade: false}); 
				var sum="";
				$.each($(".manuSum"),function(index,item){
					console.info($(item).val()+"----"+item.id);
					sum+=item.id+","+$(item).val()+"@";
				});
				var url="../../snmanufacture/updateNumber.action";
				var data={"sum":sum,"proId":proId};
				$.post(url,data,function(mes){
					layer.close(index);
					if(mes==1){
						layer.msg('保存成功', {
							icon: 1
						});

						//重载表单
						active['reload'] ? active['reload'].call(this) : '';

						//执行重载
						table.reload('manuTable', {
							page: {
								curr: 1 //重新从第 1 页开始
							},where: {
								proId: proId,
							}
						});

						$("#manuTableTwoBody").html("");
						var url="../../snmanufacture/query.action";
						var data={"proId":proId};
						$.post(url,data,function(mes){
							$.each(mes,function(index,item){
								$("#manuTableTwoBody").append("<tr>" +
										"<td>"+item.comName+"</td>" +
										"<td><input type='text' class='manuNumber' value="+item.smUndone+"></td>" +
										"<td><input type='text' id="+item.smId+" class='manuSum' value='0'></td>" +
								"</tr>");
							});
						},"json");
						load();

					}else{
						layer.msg('保存失败', {
							icon: 2
						});
					}
				},"json");
			}
	};

	$('.demoTable .layui-btn').on('click', function(){
		var type = $(this).data('type');
		active[type] ? active[type].call(this) : '';
	});

	$('#insert').on('click', function(){
		layer.open({
			type: 1	 //此处以iframe举例
			,title: '数据增加'
				,offset: 'auto'
					,area: ['500px', '300px']
		,content:$('#addOrUpdate')
		,end : function() {
			//弹框关闭的回调函数
			$("#addOrUpdate").hide();
			exit();
		}
		});
	});
	$('#sub').on('click', function(){
		findOpTime();
		findOpDate();
		var countYz=0;
		$.each($(".inputYz"),function(index,item){
			if($(item).val()=="√"){
				countYz++;
			}
		});
		console.info(countYz);
		if(countYz==2){
			var index = layer.load(0, {shade: false}); 
			var url="../../production/addOrUpdate.action";
			var data=$("#myform").serialize();
			$.post(url,data,function(mes){
				layer.close(index);
				if(mes==1){
					$("#addOrUpdate").hide();
					exit();
					parent.layer.msg('操作成功', {
						icon: 1
					});
					//重载表单
					active['reload'] ? active['reload'].call(this) : '';
				}else{
					parent.layer.msg('操作失败', {
						icon: 2
					});
				}
			},"json");
		}
	});

});
function findKeyup(id){
	if(isNaN($("#"+id).val()) || $("#"+id).val()==''){
		$("#"+id).val(0);
	}
}
function manuExit(){
	$("#comNameInput").val("");
	$("#manufactureDiv").hide();
	//关闭所有页面层
	layer.closeAll('page');
}
function load() {
	var form = layui.form
	form.render();
}

function findOpDate(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#opDate").val()=="" || $("#opDate").val()==null){
			parent.layer.msg('开始时间不能为空', {
				icon: 5,
				anim :6
			});
			$("#opDateInput").val("×");
		}else{
			$("#opDateInput").val("√");
		}
	});
}
function findOpTime(){
	layui.use(['form', 'layedit', 'laydate','table'], function(){
		var layer = layui.layer;
		if($("#opTime").val()=="" || $("#opTime").val()==null){
			parent.layer.msg('截止时间不能为空', {
				icon: 5,
				anim :6
			});
			$("#opTimeInput").val("×");
		}else{
			$("#opTimeInput").val("√");
		}
	});
}
